<template>
  <el-dialog title="质检标准增加" :visible="dialogTableVisible" @close="handleClose" :close-on-click-modal="false"
    class="dialog">
    <el-form ref="myForm" label-width="100px" :model="formData" :rules="rules">
      <el-row>
        <el-col :sm="24" :md="12">
          <el-form-item label="物料名称" prop="name">
            <el-input v-model="formData.name" readonly>
              <el-button slot="append" icon="el-icon-search" @click="nameVisible = true"></el-button>
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :sm="24" :md="12">
          <el-form-item label="检验类型" prop="check_type">
            <el-input v-model="formData.check_type" maxlength="12" />
          </el-form-item>
        </el-col>
        <el-col :sm="24" :md="12">
          <el-form-item label="标准值上限" prop="max">
            <el-input v-model="formData.max" type="number" maxlength="6" />
          </el-form-item>
        </el-col>
        <el-col :sm="24" :md="12">
          <el-form-item label="标准值下限" prop="min">
            <el-input v-model="formData.min" type="number" maxlength="6" />
          </el-form-item>
        </el-col>
        <el-col :sm="24" :md="12">
          <el-form-item label="备注" prop="remark">
            <el-input v-model="formData.remark" maxlength="256" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-button type="warning" @click="handleClose" native-type="button">取消</el-button>
        <el-button type="primary" native-type="submit" @click.prevent="handleInsert">确认</el-button>
      </el-row>
    </el-form>
    <el-dialog title="选择物料档案" :visible="nameVisible" @close="nameVisible = false" :modal="false">
      <template #default>
        <div>
          <el-form class="header">
            <el-input v-model="queryForm.name" readonly />
            <el-button @click.prevent="handleQuery" native-type="submit">查询</el-button>
          </el-form>
          <el-table :data="tabldeData" border style="width: 100%">
            <el-table-column prop="name" label="名称" width="180">
            </el-table-column>
            <el-table-column label="操作">
              <template scope="scope">
                <el-button @click="handleClick(scope.row)" type="text" size="small">选择</el-button>
              </template>
            </el-table-column>
          </el-table>
        </div>
      </template>
      <template #footer>
        <el-button type="warning" @click="nameVisible = false" native-type="button">关闭</el-button>
      </template>
    </el-dialog>
  </el-dialog>
</template>
  
<script>
export default {
  props: {
    dialogTableVisible: {
      type: Boolean,
      default: false,
      required: true
    }
  },
  data() {
    return {
      formData: {},
      queryForm: {},
      rules: {
        name: [
          { required: true, message: '请输入物料名称', trigger: 'blur' },
        ],
        check_type: [
          { required: true, message: '请输入检验类型', trigger: 'blur' },
        ],
        max: [
          { required: true, message: '请输入标准值上限', trigger: 'blur' },
        ],
        min: [
          { required: true, message: '请输入标准值下限', trigger: 'blur' },
        ],
      },
      tabldeData: [],
      nameVisible: false
    };
  },
  methods: {
    handleClose() {
      // 子传父：第一步使用$emtis发起自定义事件，并带上参数
      this.$emit("close", false);
    },
    handleInsert() {
      this.$refs['myForm'].validate((valid) => {
        if (!valid) {
          return;
        }
        console.log("校验成功了！")
        // 发起异步请求，进行新增
        // 模拟
        setTimeout(() => {
          // 新增成功
          const res = {code: 1}
          if (res.code > 0) {
            this.$message({
              message: '新增成功',
              type: 'success'
            });
            this.$emit("success");
          }
        }, 500)
      });
    },
    handleQuery() {
      setTimeout(() => {
        const arr = [];
        for (let i = 0; i < 10; i++) {
          arr.push({ "name": "物料" + i });
        }
        this.tabldeData = arr;
      }, 1000);
    },
    handleClick(row) {
      // console.log("row =",row);
      this.formData.name = row.name;
      this.nameVisible = false;
    }
  }
}
</script>
  
<style lang="scss" scoped>
.header {
  display: flex;
}
</style>